<!DOCTYPE html>
<html xmlns:th="http://www.springframework.org/schema/jdbc">
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<style>
    .layui-table-cell{
        height:auto!important;
        white-space:normal;
    }
</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="pername" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <!--<div class="layui-inline">-->
                            <!--<label class="layui-form-label">设备号</label>-->
                            <!--<div class="layui-input-inline">-->
                                <!--<input type="text" name="DeviceID" autocomplete="off" class="layui-input">-->
                            <!--</div>-->
                        <!--</div>-->
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <div class="layui-row">
            <div class="layui-col-xs3">
                <fieldset class="table-search-fieldset">
                    <legend>部门列表</legend>
                    <div id="test13" class="demo-tree-more"></div>
                </fieldset>
            </div>
            <div class="layui-col-xs9">
                <fieldset class="table-search-fieldset" style="margin-left: 10px;">
                    <legend>人员列表</legend>
                    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </fieldset>
            </div>
        </div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加人员</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除人员
                </button>
            </div>
        </script>


        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" style="margin: 0!important;" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script th:inline="javascript">
    layui.use(['form', 'table', 'tree', 'util', 'layer'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            tree = layui.tree,
            layer = layui.layer;

        table.render({
            elem: '#currentTableId',
            url: '/getPerson',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData:function(msg){
                console.log(msg);
                for (let i = 0; i < msg.data.length; i++) {
                    msg.data[i].pertype=`${msg.data[i].pertype}`.replace("0","普通人").replace("1","白名单").replace("2","黑名单");
                }
                return msg;
            },
            cols: [ [
                {field: 'id', width: 80, title: '序号'},
                {field: 'pername', width: 100, title: '姓名'},
                {field: 'gender', width: 80, title: '性别'},
                {field: 'position', width: 80, title: '职务'},
                {field: 'pertype', width: 80, title: '名单类型'},
                {field: 'idcardper', width: 180, title: '证件号'},
                {field: 'address', width: 180, title: '地址'},
                {field: 'imgdata', width:100, title: '照片', sort: false,
                    templet:function (d) { return '<div style="margin:  0 auto;text-align: center;">' +
                        '<img width="80px" height="100px" src='+d.imgdata+'></div>' }},
                {title: '操作', minWidth: 50, toolbar: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let result = JSON.stringify(data.field);
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    pername: data.field.pername
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/perManage/addPerson',
                });
                $(window).on("resize", function () {
                    layer.close(layer.index);
                    window.parent.location.reload();
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/perManage/addPerson?id=' + data.id + '', //通过id找到要编辑的人
                });
                $(window).on("resize", function () {
                    layer.close(layer.index);
                    window.parent.location.reload();
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url:`/deletePerson`,
                        type:`post`,
                        dataType:`json`,
                        data: {id:data.id},
                        success:function (res) {
                            console.log(res);
                            if (res.code == 200){
                                layer.msg('删除成功');
                                obj.del();
                                layer.close(index);
                                window.parent.location.reload();
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    });
                    return false;
                });
            }
        });

        let node = [];
        let departments = null;
        $(document).ready(function () {
            departments = [[${departments}]];
            for (let i=0; i<departments.length;i++) {
                let department = new Object();
                department.id = departments[i].id;
                department.title = departments[i].name;
                department.children  = [];
                if (departments[i].parent == 0)
                    node[departments[i].parent] = department;
                else
                    node[departments[i].parent-1].children.push(department);
            }
            console.log(node);
        });
        //无连接线风格
        tree.render({
            elem: '#test13'
            , data: node
            , contentType: "application/json"
            , showLine: false
            , click: function(item){ //点击节点回调
                // layer.msg('id：'+ item.data.id);
                table.reload('currentTableId', {
                    page: {curr: 1}
                    , where: { idcardNum: item.data.title}
                }, 'data');
                return false;
                console.log(item);
            }
        });
    });
</script>
</body>
</html>